package com.liukai.todo_android.service;

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import com.liukai.todo_android.common.utils.Constants;
import com.liukai.todo_android.ui.activity.ConcentrationActivity;


/**
 * @author ：liukai
 * @date ：Created in 2021/8/24 10:09 上午
 * @description：倒计时服务
 * @modified By：
 * @version: $
 */
public class CountDownService extends Service {
    private static final String TAG = "CountDownService";

    private IBinder mIBinder;
    private int mTime;
    private Thread mThread;
    private int status = ConcentrationActivity.STATUS_STOP;
    private int timingType;
    private boolean isCycle = true;

    public class MyBinder extends Binder {
        public CountDownService getService() {
            return CountDownService.this;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();

        mIBinder = new MyBinder();
        mThread = new Thread(() -> {
            while (isCycle) {
                if (status == ConcentrationActivity.STATUS_START) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (timingType == Constants.TIMING_TYPE_DOWN) {
                        if (mTime == 0) {
                            break;
                        }
                        mTime--;
                    }
                    if (timingType == Constants.TIMING_TYPE_UP) {
                        mTime++;
                    }
                    sendNewCountDown(mTime);
                }
            }
        });
    }

    /**
     * 发送新的倒计时到广播
     *
     * @param time
     */
    private void sendNewCountDown(int time) {
        Intent intent1 = new Intent();
        intent1.putExtra("time", getNewTime(time));
        intent1.setAction(Constants.COUNT_DOWN_BROADCAST_RECEIVER);
        sendBroadcast(intent1);
    }

    /**
     * 获取减少一秒后的时间
     *
     * @param time
     * @return
     */
    private String getNewTime(int time) {
        String hh = String.valueOf(time / 60 / 60 % 60);
        String mm = String.valueOf(time / 60 % 60);
        String ss = String.valueOf(time % 60);

        if (hh.length() == 1) {
            hh = "0" + hh;
        }
        if (mm.length() == 1) {
            mm = "0" + mm;
        }
        if (ss.length() == 1) {
            ss = "0" + ss;
        }
        return hh + ":" + mm + ":" + ss;
    }


    public CountDownService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return mIBinder;
    }

    @Override
    public void onDestroy() {
        mTime = 0;
        isCycle = false;
        mThread.interrupt();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

    @Override
    public void onRebind(Intent intent) {
        Log.e(TAG, "onRebind");
        super.onRebind(intent);
    }

    /**
     * 启动服务开启倒计时
     *
     * @param intent
     * @param flags
     * @param startId
     * @return
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        int time = intent.getIntExtra(Constants.COUNT_DOWN_TIME_KEY, 0);
        status = intent.getIntExtra(Constants.COUNT_DOWN_STATUS_KEY, -1);
        int type = intent.getIntExtra(Constants.TIMING_TYPE_KEY, -1);
        if (!mThread.isAlive()) {
            mThread.start();
            mTime = time;
            timingType = type;
        }
        if (status == ConcentrationActivity.STATUS_STOP){
            isCycle = false;
        }
        return super.onStartCommand(intent, flags, startId);
    }
}
